package com.fick.tags;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.News;

@SuppressWarnings("serial")
public class PicNewsTag extends TagSupport {

	private int maxnum = 4;

	private boolean islink = true;

	private String link = "";

	private int channelid = 0;

	@Override
	public int doStartTag() throws JspException {
		NewsDAO dao = new NewsDAO();

		String hql = " where img != '' and img is not null and isAss = '1' order by addDate desc ";
		if (channelid != 0) {
			hql = " where channelid.channelId = " + channelid
					+ " and img != '' and img is not null and isAss = '1' order by addDate desc ";
		}
		@SuppressWarnings("unchecked")
		List<News> list = dao.findPageFirstByHql(hql, maxnum, 0);
		StringBuffer sb = new StringBuffer("");
		if (list != null && list.size() > 0) {
			for (News t : list) {
				if (islink) {
					sb.append("<a href='");
					sb.append(link);
					sb.append("?newsid=");
					sb.append(t.getNewId());
					sb.append("&channelid=");
					sb.append(t.getChannelid().getChannelId());
					sb.append("'>");
				}
				sb.append("<img src='");
				sb.append(t.getImg());
				sb.append("' alt='");
				sb.append(t.getTitle());
				sb.append("'/>");
				if (islink) {
					sb.append("</a>");
				}
			}
		}

		JspWriter out = pageContext.getOut();
		try {
			out.println(sb.toString());
			out.flush();
		} catch (IOException e) {
		}
		return SKIP_BODY;
	}

	public int getChannelid() {
		return channelid;
	}

	public boolean getIslink() {
		return islink;
	}

	public String getLink() {
		return link;
	}

	public int getMaxnum() {
		return maxnum;
	}

	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}

	public void setIslink(boolean islink) {
		this.islink = islink;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public void setMaxnum(int maxnum) {
		this.maxnum = maxnum;
	}

}
